/**
 * FileName: AirUserInterceptor
 * Author:   Leopold
 * Date:     2018/9/17 15:54
 * Description:
 * History:
 */
package cn.mcsys.viewer.core.web.interceptor;

import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.modules.viewer.entity.AirViewerUser;
import cn.mcsys.viewer.core.utils.HttpRequestLocal;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class AirUserInterceptor extends HandlerInterceptorAdapter{

    private Logger logger = LoggerFactory.getLogger(AirUserInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        AirViewerUser user = HttpRequestLocal.getUserSession();
        if(user == null){
            PrintWriter writer = response.getWriter();
            JsonResult fail = JsonResult.fail();
            String s = JSON.toJSONString(fail);
            writer.write(s);
            return false;
        }
        return true;
    }
}
